/**
 * Created with JetBrains WebStorm.
 * User: sparks345
 * Date: 12-6-9
 * Time: 下午1:57
 * To change this template use File | Settings | File Templates.
 */
QZONE=window.QZONE||{};

QZONE.Finder=QZONE.Finder||{};
//QZFL.imports(QZONE.Finder.Loader.suffix+"/db.js", function(){
//
//});
QZONE.Finder.Sniffer = function() {
	/**
	 *
	 * @param container 容器
	 * @param selector 项目
	 * @param single 能获取到数据的节点
	 * @constructor
	 */
	var Filter = function(container, selector, single, btnCot) {
		this.container = container;
		this.selector = selector;
		this.single = single;
		this.btnCot = btnCot;

		Filter._pool.push(this);
	};
	Filter._pool = [];
	Filter.enumerate = function(href) {
		var _pool = Filter._pool;
		for(var i in _pool) {
			if(_pool[i].spify(href)) {
				return _pool[i].find();
			}
		}
	};

	Filter.prototype = {
		getContentWindow : function() {
			return $e(".app_canvas_frame").eq(0).contentWindow;
		},
//		getContext : QZFL.emptyFn,
		/**
		 * 过滤筛选
		 */
		find : function() {//debugger
			var me = this, bindFlag=false;
			var nodes = this.getInnerWindow ? $e("#"+this.container+" "+this.selector, this.getInnerWindow().document) : this.getContentWindow().$e("#"+this.container+" "+this.selector);
			QZFL.object.each(nodes.elements, function(node, i) {//debugger
				if($e(".finder_add", node).elements.length) {
					bindFlag=true;
					console.warn("重复绑定保护");
					return;
				}

				me.btnCot
						? me.inject($e(me.btnCot, node).eq(0))
						: me.inject(node);
			});

			!bindFlag && me.bindClick();
		},
		generateBtn : function() {
//			<a class="add_qzonefinder" href="#"><span>&#9734;</span>收藏至Qzone Finder</a>
			var btn = document.createElement("span");
			btn.className = "finder_add";
			btn.innerHTML = '<a class="add_qzonefinder" href="javascipt:;" style="font-weight:normal; font-size:12px !important;cursor:pointer;height:20px;border-radius:3px;line-height:22px;color:#000;background:#fff;padding:0 3px;-webkit-animation: add_qzonefinder .5s alternate ease-in-out;opacity:1;position: absolute;				z-index:99999;				border:1px solid #ccc;				text-decoration:none;	"><span style="color:#f60;vertical-align: top;line-height:20px;margin:0 3px 0 0;">&#9734;</span>收藏</a>';
			return btn;
		},
		/**
		 * 插入调用触发按钮
		 */
		inject : function(nd) {
			var btn = this.generateBtn();
			nd.appendChild(btn);
		},
		bindClick : function() {
			var win = this.getContentWindow(), me = this;
//debugger
			(win.QZFL && win.QZFL.event || QZFL.event).delegate(this.getInnerWindow ? $e("#"+this.container, this.getInnerWindow().document).eq(0) : win.$(this.container), this.selector+" .finder_add", "click", function(){//debugger
				var prt = this.parentNode;
				var ctt = me.single ? $e(me.single, prt).eq(0) : prt;
				me.onClick(ctt, prt, this);
			});
		},
		spify : QZFL.emptyFn,
		onClick :QZFL.emptyFn
	};



	var NoteFilter = new Filter("listArea", "li .list_tit", "span.article a");
	NoteFilter.onClick = function(ctt, prt, node) {
		var title = ctt.title;
		var link = ctt.href;
		alert([title, link]);
		QZONE.Finder.db.add({
			title : title,
			link : link
		});
	};
	NoteFilter.spify = function(href) {//debugger
		//http://user.qzone.qq.com/184868454#!app=2&pos=
		var ret = /\.qzone\.qq\.com\/\d+#!app=2&pos=catalog_list/.test(href),cot=this.getContentWindow().$(this.container);
		ret = ret && Toolkit.domVisible(cot) && QZFL.css.hasClassName(cot, "mod_list");
		ret && console.log("NOTE");
		return ret;
	}

	var NoteSummaryFilter = new Filter("listArea", "div.mod_article", null, "h4");
	NoteSummaryFilter.spify = function(href) {//debugger
		var ret = /\.qzone\.qq\.com\/\d+#!app=2&pos=catalog_list/.test(href),cot=this.getContentWindow().$(this.container);;
		ret = ret && Toolkit.domVisible(cot) && QZFL.css.hasClassName(cot, "mod_summary_wrap");
		ret && console.log("NOTE SUMMARY");
		return ret;
	};
//	NoteSummaryFilter.inject = function(nd) {
//		var btn = this.generateBtn();
//		$e("h4", nd).eq(0).appendChild(btn);
//	};
	NoteSummaryFilter.onClick = function(ctt) {
		ctt = ctt.parentNode.parentNode;
		var tmp = $e("h4 a", ctt).eq(0);
		var title = tmp.title;
		var link = tmp.href;
		var summary = $e(".bd p", ctt).eq(0).textContent;
		console.log([title, link, summary]);
//debugger
		QZFL.Finder.Loader.show(function(){
			ItemFactory.addItem({'type' : 0, 'title' : title, 'summary':summary, link:link});
		});
	};


	var AlbumFilter = new Filter("album_list_div", "li", null);
	AlbumFilter.spify = function(href){
		var ret = /\.qzone\.qq\.com\/\d+#!app=4\b/.test(href),cot=this.getContentWindow().$(this.container);
		ret = ret && Toolkit.domVisible(cot);
		ret &&console.log("ALBUM");
		return ret;//http://user.qzone.qq.com/184868454/photo/cdb062ce-75b7-4d3a-b875-f6b9faf3fb75/M0A*4aHaX0ZguoByG7ROfxsEesspLRAAAA!!/
	};
	AlbumFilter.onClick = function(ctt) {//debugger
		var href = $e("a", ctt).eq(0).href;
		var img = $e(".photo_albumlist_img img", ctt).eq(0).src;
		var title = $e(".photo_albumlist_name", ctt).eq(0).textContent;

		console.log([href,img,title]);
		QZFL.Finder.Loader.show(function(){
			ItemFactory.addItem({'type' : 1, 'title' : title, 'img':img, link:href});
		});
	};

	var PhotoFilter = new Filter("photo_list", "li", null, null);
	PhotoFilter.spify = function(href){//debugger
		var ret = /\.qzone\.qq\.com\/\d+#!app=4\b/.test(href),cot=this.getContentWindow().$(this.container);
		ret = ret && Toolkit.domVisible(cot);
		ret &&console.log("PHOTO");
		return ret;
	};
	PhotoFilter.onClick = function(ctt) {//debugger
		var title = $e(".photo_desc", ctt).eq(0).textContent;
		var img = $e("img", ctt).eq(0).src;
		console.log([title, img]);

		QZFL.Finder.Loader.show(function(){
			ItemFactory.addItem({type:1, title:title, img:img, summary:title, link:img});
		});
	};

	var ShuoshuoFilter = new Filter("msgList", "li.feed", null, "div.box");
	ShuoshuoFilter.onClick = function(ctt) {//debugger
		var title = $e(".bd", ctt).eq(0).textContent;//;
		var md = $e(".md", ctt).eq(0);
		var summary = md&&md.textContent ;
		var imgs = $e("img", ctt);
		var imgArr=[];
		for(var i=0;i<imgs.elements.length;i++){
			imgArr.push('<img src="'+imgs.elements[i].src+'" />');
		}
		summary+='</br>'+imgArr.join("");
		var href = "#";

		console.log([href, title]);
		QZFL.Finder.Loader.show(function(){
			ItemFactory.addItem({type:2, title:title, link:href, summary:summary});
		});
	},
	ShuoshuoFilter.spify = function(href){
//		http://user.qzone.qq.com/184868454#!app=311
		var ret = /\.qzone\.qq\.com\/\d+#!app=311\b/.test(href);
		ret && console.log("SHUO SHUO");
		return ret;
	};


	var MusicBGMFilter = new Filter("listDom", "tr", null);
	MusicBGMFilter.getInnerWindow = function() {//debugger
		var win = $e(".app_canvas_frame").eq(0).contentWindow;
		var mainFrame = win.$("mainFrame") && win.$("mainFrame").contentWindow;
		return mainFrame;
	},
	MusicBGMFilter.spify = function(href){//debugger
		var mat = /\.qzone\.qq\.com\/\d+#!app=305\b/.test(href);
			if(!mat) return false;
		var win = this.getContentWindow(),
			mainFrame = win.$("mainFrame") && win.$("mainFrame").contentWindow,
			cot = mainFrame.$("listDom"),
			ret = mat && Toolkit.domVisible(cot);

		ret && console.log("MUSIC BGM");

//		if((cot = win.$("listDom")) && Toolkit.domVisible(cot)){//背景音乐
//
//		}else if((cot = win.$("xx")) && Toolkit.domVisible(cot)){//我的音乐
//
//		}else if((cot = win.$("oo")) && Toolkit.domVisible(cot)){//好友音乐
//
//		}else if(cot = win.$("pp") && Toolkit.domVisible(cot)){//音乐库
//
//		}
		return ret;
	};
	MusicBGMFilter.inject = function(nd) {
		var btn = this.generateBtn();
		var td = $e("td.name", nd).eq(0);
		Toolkit.domVisible(td)&&td.appendChild(btn);
	};
	MusicBGMFilter.onClick = function(ictt) {
		var ctt = ictt.parentNode;
		var url = ctt.getAttribute("surl");
		var singer = ctt.getAttribute("ssinger");
		var name = ctt.getAttribute("sname");
		var sid = ctt.getAttribute("sid");
		var albumid = ctt.getAttribute("salbumid");
		var stype = ctt.getAttribute("stype");
		console.log([url, singer, name, sid]);
		QZFL.Finder.Loader.show(function(){//oLi.getAttribute("sname").myEncode(), oLi.getAttribute("surl"), oLi.getAttribute("sid"), oLi.getAttribute("salbumid"), oLi.getAttribute("ssinger").myEncode(), oLi.getAttribute("stype")
			ItemFactory.addItem({'type' : 3, 'title' : name, 'url':url, singer:singer, link:url, sid:sid, albumid:albumid, stype:stype});
		});
	};

	var MusicProfileFilter = new Filter("listDom_recent_listen", "li", null, null);
	MusicProfileFilter.spify = function(href) {//debugger
		var mat = /\.qzone\.qq\.com\/\d+#!app=305\b/.test(href);
			if(!mat) return false;
		var win = this.getContentWindow(),
			mainFrame = win.$("mainFrame") && win.$("mainFrame").contentWindow,
			cot = mainFrame.$(this.container),
			ret = mat && Toolkit.domVisible(cot);

		ret && console.log("MUSIC Profile");
		return ret;
	};
	MusicProfileFilter.getInnerWindow = function() {
		return MusicBGMFilter.getInnerWindow();
	},
	MusicProfileFilter.onClick = function(ictt) {//debugger
		var ctt = ictt;//.parentNode;
		var url = ctt.getAttribute("surl");
		var singer = ctt.getAttribute("ssinger");
		var name = ctt.getAttribute("sname");
		var sid = ctt.getAttribute("sid");
		var albumid = ctt.getAttribute("salbumid");
		var stype = ctt.getAttribute("stype");
		console.log([url, singer, name, sid]);
		QZFL.Finder.Loader.show(function(){
			ItemFactory.addItem({'type' : 3, 'title' : name, 'url':url, singer:singer, link:url, sid:sid, albumid:albumid, stype:stype});
		});
	};


	var Toolkit = function() {
		return {
			domVisible : function(dom) {
				var dom = $(dom);
				return dom && dom.offsetWidth>0;// && dom.offsetHeight;
			}
		}
	}();

	///////////////////////////////////////
	function bootstrap(){
		var href = location.href;
		Filter.enumerate(href);
	}
//	bootstrap();
	return {

		bootstrap : bootstrap
	}
}();


document.body.ondblclick=function() {
	QZONE.Finder.Loader.hide();
}